/**
 * ...
 * @author Franco Ponticelli
 */

package uform.model.value;

import thx.validation.DateGranularityRangeValidator;
import thx.validation.DateRangeValidator;
import utest.Assert;
import uform.model.value.DateType;
import uform.util.DateGranularity;

class TestDateType extends Test
{
/*
	public function testComparable()
	{
		var value : { private function comparable(date : Date, granularity : DateGranularity) : Float; } = new DateType();
		
		Assert.isTrue(value.comparable(Date.now(), Second) < value.comparable(DateTools.delta(Date.now(), 1000), Second));
	}
*/
	public function testParse()
	{
		var y = 2010, m = 0, d = 7, h = 19, mi = 17, s = 55;
		
		var value = new DateType(Year);
		Assert.same(new Date(y, 0, 1, 0, 0, 0), value.parse("2010"));
	
		var value = new DateType(Month);
		Assert.same(new Date(y, m, 1, 0, 0, 0), value.parse("2010-01"));
		
		var value = new DateType(Day);
		Assert.same(new Date(y, m, d, 0, 0, 0), value.parse("2010-01-07"));

		var value = new DateType(Hour);
		Assert.same(new Date(y, m, d, h, 0, 0), value.parse("2010-01-07T19"));
		
		var value = new DateType(Minute);
		Assert.same(new Date(y, m, d, h, mi, 0), value.parse("2010-01-07T19:17"));

		var value = new DateType(Second);
		Assert.same(new Date(y, m, d, h, mi, s), value.parse("2010-01-07T19:17:55"));
	}
	
	public function testValidate()
	{
		var value = new DateType();
		assertValidation(value.validate(Date.now()), true);
		var validator = new DateGranularityRangeValidator(Second, value.parse("2010-01-01T00:00:00"), value.parse("2010-12-31T23:59:59"));
		value.validators.push(validator);
		
		validator.granularity = value.granularity = Year;
		assertValidation(value.validate(value.parse("2010")), true);
		assertValidation(value.validate(value.parse("2009")), false);
		assertValidation(value.validate(value.parse("2011")), false);
		
		validator.granularity = value.granularity = Month;
		assertValidation(value.validate(value.parse("2010-12")), true);
		assertValidation(value.validate(value.parse("2010-01")), true);
		assertValidation(value.validate(value.parse("2009-12")), false);
		assertValidation(value.validate(value.parse("2011-01")), false);
		
		validator.granularity = value.granularity = Day;
		assertValidation(value.validate(value.parse("2010-12-31")), true);
		assertValidation(value.validate(value.parse("2010-01-01")), true);
		assertValidation(value.validate(value.parse("2009-12-31")), false);
		assertValidation(value.validate(value.parse("2011-01-01")), false);
		
		validator.granularity = value.granularity = Hour;
		assertValidation(value.validate(value.parse("2010-12-31T23")), true);
		assertValidation(value.validate(value.parse("2010-01-01T00")), true);
		assertValidation(value.validate(value.parse("2009-12-31T23")), false);
		assertValidation(value.validate(value.parse("2011-01-01T00")), false);
		
		validator.granularity = value.granularity = Minute;
		assertValidation(value.validate(value.parse("2010-12-31T23:59")), true);
		assertValidation(value.validate(value.parse("2010-01-01T00:00")), true);
		assertValidation(value.validate(value.parse("2009-12-31T23:59")), false);
		assertValidation(value.validate(value.parse("2011-01-01T00:00")), false);
		
		validator.granularity = value.granularity = Second;
		assertValidation(value.validate(value.parse("2010-12-31T23:59:59")), true);
		assertValidation(value.validate(value.parse("2010-01-01T00:00:00")), true);
		assertValidation(value.validate(value.parse("2009-12-31T23:59:59")), false);
		assertValidation(value.validate(value.parse("2011-01-01T00:00:00")), false);
	}

	public function testValidateType()
	{
		var value = new DateType();
		assertValidation(value.validateType(Date.now()), true);
		assertValidation(value.validateType(1), false);
	}
	
	public function testRender()
	{
		var y = 2010, m = 0, d = 7, h = 19, mi = 17, s = 55;
		
		var value = new DateType(Year);
		Assert.same("2010", value.render(new Date(y, 0, 1, 0, 0, 0)));
	
		var value = new DateType(Month);
		Assert.same("2010-01", value.render(new Date(y, m, 1, 0, 0, 0)));
		
		var value = new DateType(Day);
		Assert.same("2010-01-07", value.render(new Date(y, m, d, 0, 0, 0)));

		var value = new DateType(Hour);
		Assert.same("2010-01-07T19", value.render(new Date(y, m, d, h, 0, 0)));
		
		var value = new DateType(Minute);
		Assert.same("2010-01-07T19:17", value.render(new Date(y, m, d, h, mi, 0)));

		var value = new DateType(Second);
		Assert.same("2010-01-07T19:17:55", value.render(new Date(y, m, d, h, mi, s)));
	}
	
	public function new();
}